package com.zhangsan.seionor1.week3.auth;

import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;
import java.util.Map;

/**
 * 
 * @author zhuzg
 *
 */
@Component
public class MyScoketInterceptor implements HandshakeInterceptor {

	/***
	 * 握手之前，若返回false，则不建立链接 **
	 * 
	 * @param request
	 *            *
	 * @param response
	 *            *
	 * @param wsHandler
	 *            *
	 * @param attributes
	 *            *
	 * @return * @throws Exception
	 */
	@Override
	public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
			Map<String, Object> attributes) throws Exception {
		// 将用户id放入socket处理器的会话(WebSocketSession)中
		attributes.put("uid", 1001);
		System.out.println("MyScoketInterceptor b>>>>>>>>>>>>>>> 开始握手。。。。。。。");
		return true;
	}

	@Override
	public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
			Exception exception) {
		System.out.println("MyScoketInterceptor >>>>>>>>>>>>>> 握手成功啦。。。。。。");
	}

}
